package org.dromara.yuanshidian.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.yuanshidian.domain.bo.Ab06Bo;
import org.dromara.yuanshidian.domain.vo.Ab06Vo;
import org.dromara.yuanshidian.domain.Ab06;
import org.dromara.yuanshidian.mapper.Ab06Mapper;
import org.dromara.yuanshidian.service.IAb06Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 个人账户年度计息Service业务层处理
 *
 * @author Lion Li
 * @date 2025-01-24
 */
@RequiredArgsConstructor
@Service
public class Ab06ServiceImpl implements IAb06Service {

    private final Ab06Mapper baseMapper;

    /**
     * 查询个人账户年度计息
     *
     * @param id 主键
     * @return 个人账户年度计息
     */
    @Override
    public Ab06Vo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询个人账户年度计息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 个人账户年度计息分页列表
     */
    @Override
    public TableDataInfo<Ab06Vo> queryPageList(Ab06Bo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Ab06> lqw = buildQueryWrapper(bo);
        Page<Ab06Vo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的个人账户年度计息列表
     *
     * @param bo 查询条件
     * @return 个人账户年度计息列表
     */
    @Override
    public List<Ab06Vo> queryList(Ab06Bo bo) {
        LambdaQueryWrapper<Ab06> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Ab06> buildQueryWrapper(Ab06Bo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Ab06> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getAaa027()), Ab06::getAaa027, bo.getAaa027());
        lqw.eq(StringUtils.isNotBlank(bo.getDwh()), Ab06::getDwh, bo.getDwh());
        lqw.eq(StringUtils.isNotBlank(bo.getDw01()), Ab06::getDw01, bo.getDw01());
        lqw.eq(StringUtils.isNotBlank(bo.getDw02()), Ab06::getDw02, bo.getDw02());
        lqw.eq(StringUtils.isNotBlank(bo.getGr01()), Ab06::getGr01, bo.getGr01());
        lqw.eq(StringUtils.isNotBlank(bo.getGr02()), Ab06::getGr02, bo.getGr02());
        lqw.eq(StringUtils.isNotBlank(bo.getGr03()), Ab06::getGr03, bo.getGr03());
        lqw.eq(StringUtils.isNotBlank(bo.getGr04()), Ab06::getGr04, bo.getGr04());
        lqw.eq(StringUtils.isNotBlank(bo.getGr22()), Ab06::getGr22, bo.getGr22());
        lqw.eq(bo.getDw21() != null, Ab06::getDw21, bo.getDw21());
        lqw.eq(bo.getGr24() != null, Ab06::getGr24, bo.getGr24());
        lqw.eq(bo.getGr19() != null, Ab06::getGr19, bo.getGr19());
        lqw.eq(bo.getCs04() != null, Ab06::getCs04, bo.getCs04());
        lqw.eq(bo.getGrzh01() != null, Ab06::getGrzh01, bo.getGrzh01());
        lqw.eq(bo.getGrzh02() != null, Ab06::getGrzh02, bo.getGrzh02());
        lqw.eq(bo.getGrzh03() != null, Ab06::getGrzh03, bo.getGrzh03());
        lqw.eq(bo.getGrzh04() != null, Ab06::getGrzh04, bo.getGrzh04());
        lqw.eq(bo.getGrzh05() != null, Ab06::getGrzh05, bo.getGrzh05());
        lqw.eq(bo.getGrzh06() != null, Ab06::getGrzh06, bo.getGrzh06());
        lqw.eq(bo.getGrzh08() != null, Ab06::getGrzh08, bo.getGrzh08());
        lqw.eq(bo.getGrzh11() != null, Ab06::getGrzh11, bo.getGrzh11());
        lqw.eq(StringUtils.isNotBlank(bo.getGrjx01()), Ab06::getGrjx01, bo.getGrjx01());
        lqw.eq(bo.getGrjx02() != null, Ab06::getGrjx02, bo.getGrjx02());
        lqw.eq(bo.getGrjx03() != null, Ab06::getGrjx03, bo.getGrjx03());
        lqw.eq(bo.getGrjx04() != null, Ab06::getGrjx04, bo.getGrjx04());
        lqw.eq(bo.getGrjx05() != null, Ab06::getGrjx05, bo.getGrjx05());
        lqw.eq(bo.getGrjx06() != null, Ab06::getGrjx06, bo.getGrjx06());
        lqw.eq(bo.getGrjx07() != null, Ab06::getGrjx07, bo.getGrjx07());
        lqw.eq(bo.getGrjx08() != null, Ab06::getGrjx08, bo.getGrjx08());
        lqw.eq(bo.getGrzh16() != null, Ab06::getGrzh16, bo.getGrzh16());
        lqw.eq(bo.getGrzh17() != null, Ab06::getGrzh17, bo.getGrzh17());
        lqw.eq(bo.getGrzh18() != null, Ab06::getGrzh18, bo.getGrzh18());
        lqw.eq(bo.getGrzh19() != null, Ab06::getGrzh19, bo.getGrzh19());
        lqw.eq(bo.getGrjx10() != null, Ab06::getGrjx10, bo.getGrjx10());
        lqw.eq(bo.getGrjx11() != null, Ab06::getGrjx11, bo.getGrjx11());
        lqw.eq(bo.getGrjx12() != null, Ab06::getGrjx12, bo.getGrjx12());
        lqw.eq(bo.getGrjx13() != null, Ab06::getGrjx13, bo.getGrjx13());
        lqw.eq(bo.getGrzh21() != null, Ab06::getGrzh21, bo.getGrzh21());
        lqw.eq(bo.getGrzh22() != null, Ab06::getGrzh22, bo.getGrzh22());
        lqw.eq(bo.getGrzh23() != null, Ab06::getGrzh23, bo.getGrzh23());
        lqw.eq(bo.getGrzh24() != null, Ab06::getGrzh24, bo.getGrzh24());
        lqw.eq(bo.getGrzh25() != null, Ab06::getGrzh25, bo.getGrzh25());
        lqw.eq(bo.getGrzh26() != null, Ab06::getGrzh26, bo.getGrzh26());
        lqw.eq(bo.getGrzh27() != null, Ab06::getGrzh27, bo.getGrzh27());
        lqw.eq(bo.getGrjx22() != null, Ab06::getGrjx22, bo.getGrjx22());
        lqw.eq(bo.getGrjx23() != null, Ab06::getGrjx23, bo.getGrjx23());
        lqw.eq(bo.getGrjx24() != null, Ab06::getGrjx24, bo.getGrjx24());
        lqw.eq(bo.getGrjx25() != null, Ab06::getGrjx25, bo.getGrjx25());
        lqw.eq(bo.getGrjx26() != null, Ab06::getGrjx26, bo.getGrjx26());
        lqw.eq(bo.getGrjx27() != null, Ab06::getGrjx27, bo.getGrjx27());
        lqw.eq(StringUtils.isNotBlank(bo.getDw24()), Ab06::getDw24, bo.getDw24());
        lqw.eq(bo.getSystime() != null, Ab06::getSystime, bo.getSystime());
        lqw.eq(StringUtils.isNotBlank(bo.getKks()), Ab06::getKks, bo.getKks());
        lqw.eq(StringUtils.isNotBlank(bo.getKjc()), Ab06::getKjc, bo.getKjc());
        lqw.eq(bo.getKys() != null, Ab06::getKys, bo.getKys());
        lqw.eq(bo.getKje() != null, Ab06::getKje, bo.getKje());
        lqw.eq(bo.getKgrje() != null, Ab06::getKgrje, bo.getKgrje());
        lqw.eq(bo.getKgrzh01() != null, Ab06::getKgrzh01, bo.getKgrzh01());
        lqw.eq(bo.getKgrzh02() != null, Ab06::getKgrzh02, bo.getKgrzh02());
        lqw.eq(bo.getKgrzh03() != null, Ab06::getKgrzh03, bo.getKgrzh03());
        lqw.eq(bo.getCs02() != null, Ab06::getCs02, bo.getCs02());
        lqw.eq(bo.getCs07() != null, Ab06::getCs07, bo.getCs07());
        lqw.eq(bo.getCs24() != null, Ab06::getCs24, bo.getCs24());
        lqw.eq(bo.getGrjx09() != null, Ab06::getGrjx09, bo.getGrjx09());
        lqw.eq(bo.getGrjx29() != null, Ab06::getGrjx29, bo.getGrjx29());
        lqw.eq(bo.getGrzh14() != null, Ab06::getGrzh14, bo.getGrzh14());
        lqw.eq(bo.getStatus() != null, Ab06::getStatus, bo.getStatus());
        lqw.eq(bo.getDelStatus() != null, Ab06::getDelStatus, bo.getDelStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getFileurl()), Ab06::getFileurl, bo.getFileurl());
        return lqw;
    }

    /**
     * 新增个人账户年度计息
     *
     * @param bo 个人账户年度计息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(Ab06Bo bo) {
        Ab06 add = MapstructUtils.convert(bo, Ab06.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改个人账户年度计息
     *
     * @param bo 个人账户年度计息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(Ab06Bo bo) {
        Ab06 update = MapstructUtils.convert(bo, Ab06.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Ab06 entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除个人账户年度计息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
